/*
 * Copyright (C) 2017 Baidu, Inc. All Rights Reserved.
 */
package com.face.baidu_face.api;

import android.content.Context;

import com.baidu.idl.face.api.manager.ConsoleConfigManager;
import com.baidu.idl.face.api.model.ConsoleConfig;
//import com.baidu.ocr.sdk.model.AccessToken;
import com.face.baidu_face.OnResultListener;
import com.face.baidu_face.model.AccessToken;
import com.face.baidu_face.model.VerifyToken;
import com.face.baidu_face.utils.HttpUtil;

import java.util.HashMap;
import java.util.Map;


public class APIService {

    /**
     * ACCESS_TOKEN URL
     */
    private static final String ACCESS_TOEKN_URL = "https://aip.baidubce.com/oauth/2.0/token?";
    /**
     * VERIFY_TOKEN URL
     */
    private static final String VERIFY_TOEKN_URL = "https://aip.baidubce.com/rpc/2.0/brain/solution/faceprint/verifyToken/generate?";
    /**
     * RECOGNIZE_SUBMIT URL
     */
    private static final String RECOGNIZE_SUBMIT_URL = "https://aip.baidubce.com/rpc/2.0/brain/solution/faceprint/idcard/submit?";
    /**
     * IMAGE_MATCH URL
     */
    private static final String IMAGE_MATCH_URL = "https://aip.baidubce.com/rpc/2.0/brain/solution/faceprint/uploadMatchImage?";



    private String accessToken;
    private String verifyToken;

    private APIService() {

    }

    private static volatile APIService instance;

    public static APIService getInstance() {
        if (instance == null) {
            synchronized (APIService.class) {
                if (instance == null) {
                    instance = new APIService();
                }
            }
        }
        return instance;
    }

    // 初始化OKHttp
    public void init() {
        HttpUtil.getInstance().init();
    }

    /**
     * 设置accessToken 如何获取 accessToken 详情见:
     *
     * @param accessToken accessToken
     */
    public void setAccessToken(String accessToken) {
        this.accessToken = accessToken;
    }

    public String getAccessToken() {
        return accessToken;
    }

    public void setVerifyToken(String verifyToken) {
        this.verifyToken = verifyToken;
    }

    public String getVerifyToken() {
        return verifyToken;
    }

    /**
     * 明文ak/sk获取token
     *
     * @param listener 回调监听
     * @param ak       access key
     * @param sk       secret key
     */
    public void initAccessTokenWithAkSk(final OnResultListener<AccessToken> listener, String ak, String sk) {
        StringBuilder sb = new StringBuilder();
        sb.append("client_id=").append(ak);
        sb.append("&client_secret=").append(sk);
        sb.append("&grant_type=client_credentials");
        HttpUtil.getInstance().getAccessToken(listener, ACCESS_TOEKN_URL, sb.toString());
    }

    public void initVerifyToken(Context context, final OnResultListener<VerifyToken> listener, String matchSource) {
        ConsoleConfig consoleConfig = ConsoleConfigManager.getInstance(context).getConfig();
        Map<String, Object> params = new HashMap<String, Object>();
        // params.put("access_token", accessToken);
        params.put("plan_id", consoleConfig.getPlanId());
        params.put("match_source", matchSource);
        HttpUtil.getInstance().getVerifyToken(listener, VERIFY_TOEKN_URL + "access_token=" + accessToken, params);
    }

    /**
     * 二要素身份信息上报
     */
    public void recognizeSubmit(Map<String, Object> params, final OnResultListener<String> listener) {
        HttpUtil.getInstance().recognizeSubmit(listener, RECOGNIZE_SUBMIT_URL + "access_token=" + accessToken, params);
    }

    /**
     * 对比图片上传接口
     */
    public void imageMatch(Map<String, Object> params, final OnResultListener<String> listener) {
        HttpUtil.getInstance().imageMatch(listener, IMAGE_MATCH_URL + "access_token=" + accessToken, params);
    }
}
